package com.vertx.codec;

import com.vertx.model.dto.OtherDTO;
import com.vertx.model.vo.OtherVO;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.MessageCodec;

import java.io.*;

/**
 * @name: OtherCodec
 * @description: OtherCodec
 * @author: jiangcs
 */
public class OtherCodec implements MessageCodec<OtherDTO, OtherVO> {
    /**
     * 把参数s进行序列化添加到buffer中，该方法用于集群模式
     *
     * @param buffer
     * @param otherDTO
     */
    @Override
    public void encodeToWire(Buffer buffer, OtherDTO otherDTO) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(otherDTO);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != objectOutputStream) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        buffer.appendBytes(byteArrayOutputStream.toByteArray());
        System.out.println("encode to wire");
    }

    /**
     * 从buffer读取数据，反序列化出对象，该方法用于集群模式
     *
     * @param pos
     * @param buffer
     * @return
     */
    @Override
    public OtherVO decodeFromWire(int pos, Buffer buffer) {
        OtherVO otherVO = null;

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buffer.getBytes());
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            OtherDTO otherDTO = (OtherDTO) objectInputStream.readObject();

            otherVO = new OtherVO();
            otherVO.setUserName(otherDTO.getUserName());
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != objectInputStream) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("decode from wire");
        return otherVO;
    }

    /**
     * 对消息参数进行处理. 例如发送Json参数发生copy，正是用该参数处理的。
     * 该方法主要对参数进行线程安全处理
     *
     * @param otherDTO
     * @return
     */
    @Override
    public OtherVO transform(OtherDTO otherDTO) {
        OtherVO otherVO = new OtherVO();
        otherVO.setUserName(otherDTO.getUserName());
        System.out.println("transform");
        return otherVO;
    }

    /**
     * 消息编解码器的名称，该名称必须唯一
     *
     * @return
     */
    @Override
    public String name() {
        return "otherCodec";
    }

    /**
     * Used to identify system codecs. Should always return -1 for a user codec.
     *
     * @return
     */
    @Override
    public byte systemCodecID() {
        return -1;
    }
}
